﻿using System;
using System.Collections;
using System.Collections.Generic;
using UniRx.Triggers;
using UnityEngine;

namespace UniRx
{
	// Token: 0x020003E0 RID: 992
	public static class ObserveExtensions
	{
		// Token: 0x06001468 RID: 5224 RVA: 0x0005B893 File Offset: 0x00059C93
		public static IObservable<TProperty> ObserveEveryValueChanged<TSource, TProperty>(this TSource source, Func<TSource, TProperty> propertySelector, FrameCountType frameCountType = FrameCountType.Update, bool fastDestroyCheck = false) where TSource : class
		{
			return source.ObserveEveryValueChanged(propertySelector, frameCountType, UnityEqualityComparer.GetDefault<TProperty>(), fastDestroyCheck);
		}

		// Token: 0x06001469 RID: 5225 RVA: 0x0005B8A3 File Offset: 0x00059CA3
		public static IObservable<TProperty> ObserveEveryValueChanged<TSource, TProperty>(this TSource source, Func<TSource, TProperty> propertySelector, FrameCountType frameCountType, IEqualityComparer<TProperty> comparer) where TSource : class
		{
			return source.ObserveEveryValueChanged(propertySelector, frameCountType, comparer, false);
		}

		// Token: 0x0600146A RID: 5226 RVA: 0x0005B8B0 File Offset: 0x00059CB0
		public static IObservable<TProperty> ObserveEveryValueChanged<TSource, TProperty>(this TSource source, Func<TSource, TProperty> propertySelector, FrameCountType frameCountType, IEqualityComparer<TProperty> comparer, bool fastDestroyCheck) where TSource : class
		{
			return null;
		}

		// Token: 0x0600146B RID: 5227 RVA: 0x0005B990 File Offset: 0x00059D90
		private static IEnumerator EmptyEnumerator()
		{
			yield break;
		}

		// Token: 0x0600146C RID: 5228 RVA: 0x0005B9A4 File Offset: 0x00059DA4
		private static IEnumerator PublishPocoValueChanged<TSource, TProperty>(WeakReference sourceReference, TProperty firstValue, Func<TSource, TProperty> propertySelector, IEqualityComparer<TProperty> comparer, IObserver<TProperty> observer, CancellationToken cancellationToken)
		{
			TProperty currentValue = default(TProperty);
			TProperty prevValue = firstValue;
			while (!cancellationToken.IsCancellationRequested)
			{
				object target = sourceReference.Target;
				if (target == null)
				{
					observer.OnCompleted();
					yield break;
				}
				try
				{
					currentValue = propertySelector((TSource)((object)target));
				}
				catch (Exception error)
				{
					observer.OnError(error);
					yield break;
				}
				finally
				{
					target = null;
				}
				if (!comparer.Equals(currentValue, prevValue))
				{
					observer.OnNext(currentValue);
					prevValue = currentValue;
				}
				yield return null;
			}
			yield break;
		}

		// Token: 0x0600146D RID: 5229 RVA: 0x0005B9E4 File Offset: 0x00059DE4
		private static IEnumerator PublishUnityObjectValueChanged<TSource, TProperty>(UnityEngine.Object unityObject, TProperty firstValue, Func<TSource, TProperty> propertySelector, IEqualityComparer<TProperty> comparer, IObserver<TProperty> observer, CancellationToken cancellationToken, bool fastDestroyCheck)
		{
			TProperty currentValue = default(TProperty);
			TProperty prevValue = firstValue;
			TSource source = (TSource)((object)unityObject);
			if (fastDestroyCheck)
			{
				ObservableDestroyTrigger destroyTrigger = null;
				GameObject gameObject = unityObject as GameObject;
				if (gameObject == null)
				{
					Component component = unityObject as Component;
					if (component != null)
					{
						gameObject = component.gameObject;
					}
				}
				if (!(gameObject == null))
				{
					destroyTrigger = ObserveExtensions.GetOrAddDestroyTrigger(gameObject);
					while (!cancellationToken.IsCancellationRequested)
					{
						bool isDestroyed = (!destroyTrigger.IsActivated) ? (unityObject != null) : (!destroyTrigger.IsCalledOnDestroy);
						if (!isDestroyed)
						{
							observer.OnCompleted();
							yield break;
						}
						try
						{
							currentValue = propertySelector(source);
						}
						catch (Exception error)
						{
							observer.OnError(error);
							yield break;
						}
						if (!comparer.Equals(currentValue, prevValue))
						{
							observer.OnNext(currentValue);
							prevValue = currentValue;
						}
						yield return null;
					}
					yield break;
				}
			}
			while (!cancellationToken.IsCancellationRequested)
			{
				if (!(unityObject != null))
				{
					observer.OnCompleted();
					yield break;
				}
				try
				{
					currentValue = propertySelector(source);
				}
				catch (Exception error2)
				{
					observer.OnError(error2);
					yield break;
				}
				if (!comparer.Equals(currentValue, prevValue))
				{
					observer.OnNext(currentValue);
					prevValue = currentValue;
				}
				yield return null;
			}
			yield break;
		}

		// Token: 0x0600146E RID: 5230 RVA: 0x0005BA2C File Offset: 0x00059E2C
		private static ObservableDestroyTrigger GetOrAddDestroyTrigger(GameObject go)
		{
			ObservableDestroyTrigger observableDestroyTrigger = go.GetComponent<ObservableDestroyTrigger>();
			if (observableDestroyTrigger == null)
			{
				observableDestroyTrigger = go.AddComponent<ObservableDestroyTrigger>();
			}
			return observableDestroyTrigger;
		}
	}
}
